#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import sys

execfile(os.path.join(sys.path[0], "framework.py"))

from oriolus.pith.packets.hashwriter import  HashFileWriter, Strategy, Visitor
from unittest import TestCase, TestLoader, TextTestRunner

class TestHashFileWriter(TestCase):

    def setUp(self):
        self.__path = os.path.abspath("testdata/packets/packet_1")
        self.__checksume_file = os.path.join(self.__path, "CHECKSUMS")
        
        self.__visitor = Visitor(self.__path)
        self.__strategy = Strategy() 

    def tearDown(self):
        if os.path.exists(self.__checksume_file):
            os.remove(self.__checksume_file)
        
    def mock_action(self, file_name):
        return "***"

    def test_writer1(self):
        dict = self.__visitor.list(self.__strategy.get_file_hash)

        writer = HashFileWriter(self.__checksume_file)
        writer.write_strings(dict)

        fd = file(self.__checksume_file)
        try:
            for line in fd:
                lines = line.split('\t')
                file_name = lines[1]
                key = file_name[:len(file_name)-1]
                self.assertEqual(dict[key], lines[0])
        finally:
            if fd: fd.close()
    
            
    def test_visitor_1(self):        
        dict = self.__visitor.list(self.mock_action)
        self.assertEqual(10, len(dict))
        etalon_list = ["META", "data/file1.txt", "data/file2.txt", "scripts/install.py", "scripts/postinstall.py", "scripts/postremove.py", "scripts/preinstall.py", "scripts/preremove.py", "scripts/remove.py"]
        for filename in etalon_list:
            self.assertTrue(filename in dict)
        

if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestHashFileWriter)
    TextTestRunner(verbosity=2).run(suite)


